﻿using System;

namespace Assignment4
{
    class Program
    {
        static void Main(string[] args)
        {
            // 整型List
            GenericList<int> intlist = new GenericList<int>(); 
            for (int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }
            int sum = 0;
            int max = 0;
            int min = 0;
            intlist.ForEach(i => sum +=i);
            intlist.ForEach(m => max = (max < m) ? m : max);
            intlist.ForEach(m => min = (min > m) ? m : min);
            Console.WriteLine($"sum, max, min of intlist are: {sum}, {max}, {min}.");


        }
    }
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null; Data = t;
        }
    }
    public class GenericList<T>
    {
        private Node<T> head; private Node<T> tail;
        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head { get => head; }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t); if (tail == null) { head = tail = n; }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            if (head == null) return;
            Node<T> p=head;
            do
            {
                action(p.Data);
                p = p.Next;
            } while (p!=null);
        }
    }
}
